package com.maduixa.guitar.tabsearch.spring.web;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HttpServletBean;
import org.springframework.web.servlet.ModelAndView;

import com.maduixa.guitar.tabsearch.spring.business.model.GuitarUser;
import com.maduixa.guitar.tabsearch.spring.business.model.Song;


@Controller
@Scope ("session")
@RequestMapping({"/songs/*"})
public class SongsViewController extends HttpServletBean{

	private static final long serialVersionUID = -7981219728872367941L;

	@Autowired com.maduixa.guitar.tabsearch.spring.business.SongsController domainController;
	
	@RequestMapping(value="/search",method=RequestMethod.GET)
    public ModelAndView search(){
		if(!logged()) return new ModelAndView("redirect:/users/login.html");
    	
    	return new ModelAndView("songs/searchSongPg");
    }
	
	@RequestMapping(value="/doSearch",method=RequestMethod.POST)
    public ModelAndView doSearch(
    		@RequestParam("artist") String artist,
    		@RequestParam("song") String song,
    		@RequestParam("tune") String tune
    		){
		if(!logged()) return new ModelAndView("redirect:/users/login.html");
		
    	ModelAndView result = new ModelAndView("songs/listSongsPg");
    	
    	GuitarUser gu = (GuitarUser) session().getAttribute("user");
    	
    	session().setAttribute("artistSearch", artist);
    	session().setAttribute("songSearch", song);
    	session().setAttribute("tuneSearch", tune);
    	
    	result.addObject("songs", domainController.getSongs(gu.getUsername(),artist,song,tune,null, null));
    	
    	return result;
    }
	
	@RequestMapping(value="/doSearchSort",method=RequestMethod.GET)
    public ModelAndView doSearchSort(
    		@RequestParam(value ="order", required = false) String order,
    		@RequestParam(value ="direction", required = false) String direction
    		){
		if(!logged()) return new ModelAndView("redirect:/users/login.html");
		
    	ModelAndView result = new ModelAndView("songs/listSongsPg");
    	
    	GuitarUser gu = (GuitarUser) session().getAttribute("user");
    	
    	String artist=(String) session().getAttribute("artistSearch");
		String song=(String) session().getAttribute("songSearch");
		String tune=(String) session().getAttribute("tuneSearch");
		
		result.addObject("songs", domainController.getSongs(gu.getUsername(),artist,song,tune,order,direction));
    	
    	return result;
    }
    
    @RequestMapping(value="/{id}",method=RequestMethod.GET)
    public ModelAndView mySongs(@PathVariable("id") long id){
    	if(!logged()) return new ModelAndView("redirect:/users/login.html");
    	
    	ModelAndView result = new ModelAndView("songs/songPg");
    	
    	Song s = domainController.getSong(id);
    	result.addObject(s);
    	
    	return result;
    }
    
    @RequestMapping(value="/addSong",method=RequestMethod.GET)
    public ModelAndView addSong(){
    	if(!logged()) return new ModelAndView("redirect:/users/login.html");
    	
    	return new ModelAndView("songs/editSongPg");
    }
    
    @RequestMapping(value="/edit{id}",method=RequestMethod.GET)
    public ModelAndView editSong(@PathVariable("id") long id){
    	if(!logged()) return new ModelAndView("redirect:/users/login.html");
    	
    	ModelAndView result = new ModelAndView("songs/editSongPg");
    	
    	Song s = domainController.getSong(id);
    	result.addObject(s);
    	
    	return result;
    }
    
    @RequestMapping(value="/remove{id}",method=RequestMethod.POST)
    public ModelAndView removeSong(@PathVariable("id") long id){
    	if(!logged()) return new ModelAndView("redirect:/users/login.html");
    	
    	GuitarUser gu = (GuitarUser) session().getAttribute("user");
    	
    	if (gu.isAdmin()){
	    	if (domainController.checkSongUsage(id)){
	    		ModelAndView result = new ModelAndView("/error/errorPg");
		    	result.addObject("error", "Someone is playing this song");
		    	return result;
	    	}
	    	else {
	    		domainController.removeSong(id);
	    		return new ModelAndView("songs/editSongPg");
	    	}
    	}
    	else {
    		ModelAndView result = new ModelAndView("/error/errorPg");
	    	result.addObject("error", "Only an administrator can delete this song");
	    	return result;
    	}
    }
    
    @RequestMapping(value="/saveSong",method=RequestMethod.POST)
    public ModelAndView saveSong(
    		@RequestParam("id") long id,
    		@RequestParam("artist") String artist,
    		@RequestParam("song") String song,
    		@RequestParam("tune") String tune,
    		@RequestParam("coments") String coments,
    		@RequestParam("video") String video,
    		@RequestParam("guitar1") Integer guitar1,
    		@RequestParam("guitar2") Integer guitar2,
    		@RequestParam("guitar3") Integer guitar3,
    		@RequestParam("bass") Integer bass,
    		@RequestParam("solo") Integer solo,
    		@RequestParam("tabLink") String tabLink,
    		@RequestParam("tabName") String tabName,
    		@RequestParam("songsterr") String songsterr,
    		@RequestParam("summary") String summary
    		){
    	
    	if(!logged()) return new ModelAndView("redirect:/users/login.html");
    	
    	ModelAndView result = new ModelAndView();
    	if (id == 0){
    		Song s = domainController.addSong(artist, song, tune, coments, video, guitar1, guitar2, guitar3,
    				bass, solo, tabLink, tabName, songsterr, summary);
    		result.setViewName("redirect:/songs/"+s.getId()+".html");
    	}
    	else {
    		domainController.updateSong(id, artist, song, tune, coments, video, guitar1, guitar2, guitar3,
    				bass, solo, tabLink, tabName, songsterr, summary);
    		result.setViewName("redirect:/songs/"+id+".html");
    	}
    	return result;
    }

    public static HttpSession session() {
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        return attr.getRequest().getSession(true); // true == allow create
    }
    
    private boolean logged() {
		if(UsersViewController.session().getAttribute("user") != null)
			return true;
		else
			return false;
	}
}